<?php

function loadBreadcrumb($params) {
    $delimiter = ' > ';
    $playlist_name = ( isset($params->playlist_name) ) ? '<a href="' . base_url() . 'chapter/mchapter/index/' . $params->playlist_id . '">' . $params->playlist_name . '</a>' . $delimiter : '';
    $parent_chapter_name = ( isset($params->parent_chapter_name) ) ? $params->parent_chapter_name . $delimiter : '';
    $chapter_name = ( isset($params->chapter_name) ) ? '<a href="' . base_url() . 'display/index/' . $params->chapter_id . '">' . $params->chapter_name . '</a>' . $delimiter : '';
    $result = $playlist_name . $parent_chapter_name . $chapter_name;
    return $result;
}

function loadBreadcrumbT($params) {
    $delimiter = ' > ';
    $funeral_name = ( isset($params->funeral_name) ) ? '<a href="' . base_url() . 'playlist/tplaylist/index/' . $params->content_id . '">' . char_limiter($params->funeral_name, 10) . '</a>' . $delimiter : '';
    $playlist_name = ( isset($params->playlist_name) ) ? '<a href="' . base_url() . 'chapter/tchapter/index/' . $params->playlist_id . '">' . $params->playlist_name . '</a>' . $delimiter : '';
    $parent_chapter_name = ( isset($params->parent_chapter_name) ) ? $params->parent_chapter_name . $delimiter : '';
    $chapter_name = ( isset($params->chapter_name) ) ? '<a href="' . base_url() . 'display/tdisplay/index/' . $params->chapter_id . '">' . $params->chapter_name . '</a>' . $delimiter : '';
    $result = $funeral_name . $playlist_name . $parent_chapter_name . $chapter_name;
    return $result;
}

function loadBreadcrumbTB($params) {
    $delimiter = ' > ';
    $funeral_name = ( isset($params->funeral_name) ) ? '<a href="' . base_url() . 'playlist/tplaylist/index/' . $params->content_id . '">' . char_limiter($params->funeral_name, 10) . '</a>' . $delimiter : '';
    $playlist_name = ( isset($params->playlist_name) ) ? '<a href="' . base_url() . 'playlist/tplaylistbackup/index/' .$params->content_id.'/' . $params->playlist_id . '">' . $params->playlist_name . '</a>' . $delimiter : '';
    $parent_chapter_name = ( isset($params->parent_chapter_name) ) ? $params->parent_chapter_name . $delimiter : '';
    $chapter_name = ( isset($params->chapter_name) ) ? '<a href="' . base_url() . 'display/tdisplay/index/' . $params->chapter_id . '">' . $params->chapter_name . '</a>' . $delimiter : '';
    $result = $funeral_name . $playlist_name . $parent_chapter_name . $chapter_name;
    return $result;
}

function loadBreadcrumbC($params) {
    $delimiter = ' > ';
    $cpattern_name = ( isset($params->cpattern_name) ) ? '<a href="' . base_url() . 'cpattern/cpatterndetail/index/' . $params->cpattern_id . '">' . $params->cpattern_name . '</a>' . $delimiter : '';
    return $cpattern_name;
}

/**
 * Format bytes
 * @param type $bytes
 * @param type $precision
 * @return string
 */
function formatBytes($bytes, $precision = 2) {
    if ($bytes == 0) {
        return $bytes . 'MB';
    } else if ($bytes < 0) {
        $bytes = str_replace('-', '', $bytes);

        $base = log($bytes) / log(1024);
        $suffixes = array('B', 'KB', 'MB', 'GB', 'TB');
        return '-' . round(pow(1024, $base - floor($base)), $precision) . $suffixes[floor($base)];
    }
    $base = log($bytes) / log(1024);
    $suffixes = array('B', 'KB', 'MB', 'GB', 'TB');
    return round(pow(1024, $base - floor($base)), $precision) . $suffixes[floor($base)];
}

/**
 * Get Size of folder
 * @param type $dir
 * @return type
 */
function foldersize($dir) {
    if (!is_dir($dir)) {
        return array(0, 0);
    }

    $count_size = 0;
    $count = 0;
    $dir_array = scandir($dir);
    foreach ($dir_array as $key => $filename) {
        if ($filename != ".." && $filename != ".") {
            if (is_dir($dir . "/" . $filename)) {
                $new_foldersize = foldersize($dir . "/" . $filename);
                $count_size = $count_size + $new_foldersize[0];
                $count = $count + $new_foldersize[1];
            } else if (is_file($dir . "/" . $filename)) {
                $count_size = $count_size + filesize($dir . "/" . $filename);
                $count++;
            }
        }
    }

    return array($count_size, $count);
}

/**
 * Write Log File
 *
 * Generally this function will be called using the global log_message() function
 *
 * @param string $level
 * @param $msg
 * @param bool $php_error
 * @param string $type_path
 * @param $file_name
 * @param string $proccess
 * @param string $action
 * @return bool
 */
function write_log($level = 'error', $msg, $php_error = FALSE, $type_path = 'server', $file_name, $proccess = '', $action = '') {
    $type_path = strtoupper($type_path);
    switch ($type_path) {
        case 'SERVER':
            $file_path = UPLOADPATH . 'logs/sn_s';
            break;
        case 'CONTROLLER':
            $file_path = UPLOADPATH . 'logs/sn_c';
            break;
        case 'MONITOR':
            $file_path = UPLOADPATH . 'logs/sn_m';
            break;
        default:
            $file_path = UPLOADPATH . 'logs/sn_s';
            break;
    }

    //Make new directory
    $filename = ($file_path . "/");
    //create new parts category if it has not existed yet
    if (!file_exists($filename)) {
        mkdir($file_path, 0777);
    }
    $file_path = $file_path . '/';
    //end make new directory

    $file_path .=$file_name;
    $message = '';

    if (!file_exists($file_path)) {
        $message .= "";
    }

//    if(file_exists($file_path)){
//        unlink($file_path);
//    }

    if (!$fp = @fopen($file_path, FOPEN_WRITE_CREATE)) {
        return FALSE;
    }

    $message .= date('Y/m/d h:m:s') . ' [ ' . $proccess . ' ] ' . $action . $msg . "\n";

    flock($fp, LOCK_EX);
    fwrite($fp, $message);
    flock($fp, LOCK_UN);
    fclose($fp);

    @chmod($file_path, FILE_WRITE_MODE);
    return TRUE;
}

function is_image($image) {
    $image_mimes = array('gif', 'jpg', 'jpeg', 'png', 'bmp');
    $arr = explode('.', $image);
    if (in_array(strtolower(end($arr)), $image_mimes)) {
        return true;
    }
    return false;
}

/**
 * Add files to zip
 * @param unknown_type $data file array
 * @param unknown_type $pathfolder
 * @param unknown_type $filename
 * @param unknown_type $keep_last keep last directory name
 */
function zipFiles($data = array(), $pathfolder, $filename, $keep_last = false) {
    $zip = new ZipArchive();
    $zip->open($pathfolder . $filename, ZIPARCHIVE::OVERWRITE);
    foreach ($data as $file) {
        if (file_exists($file)) {
            $last_folder = '';
            $paths = explode('/', $file);
            if ($keep_last === TRUE) {
                if (count($paths) > 2)
                    $last_folder = $paths[count($paths) - 2] . '/';
            }
            $file_name = end($paths);
            $zip->addFile($file, $last_folder . $file_name);
        }
    }
    $zip->close();
}

function sortChapter($chapter_a, $chapter_b) {
    return $chapter_a['parent_id'] - $chapter_b['parent_id'];
}

function char_limiter($string, $size = 20, $end_char = '...') {
    if (strlen($string) <= $size) {
        $end_char = '';
    }
    return mb_substr($string, 0, $size) . $end_char;
}

?>
